unPrincipal;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ZConnection, StdCtrls, DB, ZAbstractRODataset, ZAbstractDataset,
  ZAbstractTable, ZDataset, Grids, DBGrids, ComCtrls, ExtCtrls;

type
  TfmPrincipal = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    Button1: TButton;
    cbProtocoloBDFonte: TComboBox;
    Button2: TButton;
    cbProtocoloBDDestino: TComboBox;
    zCon1: TZConnection;
    zCon2: TZConnection;
    tbl1: TZTable;
    tbl2: TZTable;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    TabSheet2: TTabSheet;
    edQuery1: TEdit;
    Button11: TButton;
    Button10: TButton;
    edquery2: TEdit;
    Label7: TLabel;
    DBGrid1: TDBGrid;
    Label8: TLabel;
    DBGrid2: TDBGrid;
    Label5: TLabel;
    lbTabelas1: TListBox;
    lbTabelas2: TListBox;
    Label2: TLabel;
    lbCampos2: TListBox;
    Label6: TLabel;
    Label1: TLabel;
    lbCampos1: TListBox;
    Button3: TButton;
    Button4: TButton;
    Button8: TButton;
    Button5: TButton;
    Button6: TButton;
    Button9: TButton;
    Button7: TButton;
    Shape1: TShape;
    Label9: TLabel;
    Label10: TLabel;
    edPortaBDFonte: TEdit;
    Label11: TLabel;
    edUsuarioBDFonte: TEdit;
    Label12: TLabel;
    edSenhaBDFonte: TEdit;
    Label13: TLabel;
    edPortaBDDestino: TEdit;
    Label14: TLabel;
    edUsuarioBDDestino: TEdit;
    edSenhaBDDestino: TEdit;
    Label15: TLabel;
    Label16: TLabel;
    Label3: TLabel;
    edBase1: TEdit;
    Label4: TLabel;
    edBase2: TEdit;
    Label17: TLabel;
    Edit7: TEdit;
    Label18: TLabel;
    Edit8: TEdit;
    Button12: TButton;
    Button13: TButton;
    edHostBDFonte: TEdit;
    Label19: TLabel;
    edHostBDDestino: TEdit;
    Label20: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure lbTabelas1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure lbTabelas1Click(Sender: TObject);
    procedure lbTabelas2Click(Sender: TObject);
    procedure lbTabelas2KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button12Click(Sender: TObject);
    procedure Button13Click(Sender: TObject);
  private
    procedure ExibirCamposTbl1;
    procedure ExibirCamposTbl2;
    function getQueryPorNome(Nome: string): tzquery;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fmPrincipal: TfmPrincipal;
  ListaDeQuerys: TList;

implementation

{$R *.dfm}

procedure TfmPrincipal.Button1Click(Sender: TObject);
begin
  zCon1.Connect;
  zCon1.Database := edBase1.Text;
  zCon1.Protocol := cbProtocoloBDFonte.Text;
  zCon1.Port := StrToInt(edPortaBDFonte.Text);
  zCon1.HostName := edHostBDFonte.Text;
  zCon1.User := edUsuarioBDFonte.Text;
  zCon1.Password := edSenhaBDFonte.Text;
  try
    zCon1.Connect;
    zCon1.GetTableNames('', lbTabelas1.Items);
    ShowMessage('Base conectada com sucesso!');
  except
    ShowMessage('Erro ao conectar!');
  end;
end;

procedure TfmPrincipal.Button2Click(Sender: TObject);
begin
  zCon2.Database := edBase2.Text;
  zCon2.Protocol := cbProtocoloBDDestino.Text;
  zCon2.HostName := edHostBDDestino.Text;
  zCon2.Port := StrToInt(edPortaBDDestino.Text);
  zCon2.User := edUsuarioBDDestino.Text;
  zCon2.Password := edSenhaBDDestino.Text;
  try
    zCon2.Connect;
    zCon2.GetTableNames('', lbTabelas2.Items);
    ShowMessage('Base conectada com sucesso!');
  except
    ShowMessage('Erro ao conectar!');
  end;
end;

procedure TfmPrincipal.lbTabelas1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
ExibirCamposTbl1;
end;

procedure TfmPrincipal.ExibirCamposTbl1;
var
  qry: tzquery;
begin
  if POS('QRY_DNMC_', lbTabelas1.Items[lbTabelas1.ItemIndex]) = 0 then
  begin
    tbl1.TableName := lbTabelas1.Items[lbTabelas1.ItemIndex];
    tbl1.open;
    tbl1.GetFieldNames(lbCampos1.Items);
    lbCampos1.ItemIndex := 0;
  end
  else
  begin
    qry := getQueryPorNome(lbTabelas1.Items[lbTabelas1.ItemIndex]);
    DataSource1.DataSet := qry;
    qry.GetFieldNames(lbCampos1.Items);
    lbCampos1.ItemIndex := 0;
  end;
end;

function TfmPrincipal.getQueryPorNome(Nome: string):tzquery;
var
  i: integer;
begin
  result := nil;
  for i :=0 to ListaDeQuerys.Count -1 do
  begin
    if tzquery(ListaDeQuerys.Items[i]).name = nome then
      result := tzquery(ListaDeQuerys.Items[i]);
  end;
end;

procedure TfmPrincipal.ExibirCamposTbl2;
begin
  tbl2.TableName := lbTabelas2.Items[lbTabelas2.ItemIndex];
  tbl2.open;
  tbl2.GetFieldNames(lbCampos2.Items);
  lbCampos2.ItemIndex := 0;
end;

procedure TfmPrincipal.lbTabelas1Click(Sender: TObject);
begin
ExibirCamposTbl1;
end;

procedure TfmPrincipal.lbTabelas2Click(Sender: TObject);
begin
ExibirCamposTbl2;
end;

procedure TfmPrincipal.lbTabelas2KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
ExibirCamposTbl2;
end;

procedure TfmPrincipal.Button3Click(Sender: TObject);
var
 i: integer;
begin
  i := lbCampos1.ItemIndex;
  lbCampos1.Items.Move(i, i - 1);
  lbCampos1.ItemIndex :=  i - 1;
end;

procedure TfmPrincipal.Button4Click(Sender: TObject);
var
 i: integer;
begin
  i := lbCampos1.ItemIndex;
  lbCampos1.Items.Move(i, i + 1);
  lbCampos1.ItemIndex :=  i + 1;
end;

procedure TfmPrincipal.Button7Click(Sender: TObject);
var
 c: integer;
 qry1:tzquery;
begin
  if POS('QRY_DNMC_', lbTabelas1.Items[lbTabelas1.ItemIndex]) = 0 then
  begin
    tbl1.First;
    while not tbl1.Eof do
    begin
      tbl2.Append;
      for c := 0 to lbCampos1.Count - 1 do
        tbl2.FieldByName(lbCampos1.Items[c]).Value := tbl1.FieldByName(lbCampos2.Items[c]).Value;
      tbl2.Post;
      tbl1.Next;
    end;
    tbl1.Refresh;
    tbl2.Refresh;
  end
  else
  begin
    qry1 := getQueryPorNome(lbTabelas1.Items[lbTabelas1.itemindex]);
    qry1.First;
    while not qry1.Eof do
    begin
      tbl2.Append;
      for c := 0 to lbCampos1.Count - 1 do
        tbl2.FieldByName(lbCampos2.Items[c]).Value := qry1.FieldByName(lbCampos1.Items[c]).Value;
      tbl2.Post;
      qry1.Next;
    end;
    qry1.Refresh;
    tbl2.Refresh;
  end;
end;

procedure TfmPrincipal.Button9Click(Sender: TObject);
begin
  lbCampos2.Items.Delete(lbCampos2.ItemIndex);
end;

procedure TfmPrincipal.Button8Click(Sender: TObject);
begin
lbCampos1.Items.Delete(lbCampos1.ItemIndex);
end;

procedure TfmPrincipal.Button10Click(Sender: TObject);
begin
  ShowMessage(lbCampos1.Items[2]);
end;

procedure TfmPrincipal.Button11Click(Sender: TObject);
var
  qry: tzquery;
begin
  qry := TZQuery.Create(Application);
  qry.Name := 'QRY_DNMC_' + IntToStr(ListaDeQuerys.Count);
  QRY.SQL.text := edQuery1.Text;
  qry.Connection := zCon1;
  qry.Open;

  ListaDeQuerys.Add(qry);
  lbTabelas1.Items.Add(qry.Name);
end;

procedure TfmPrincipal.FormCreate(Sender: TObject);
begin
 ListaDeQuerys := tlist.create();
end;

procedure TfmPrincipal.Button12Click(Sender: TObject);
begin
  zCon1.Disconnect;
  ShowMessage('Base Desconectada com sucesso!');
end;

procedure TfmPrincipal.Button13Click(Sender: TObject);
begin
  zCon2.Disconnect;
  ShowMessage('Base Desconectada com sucesso!');
end;


end.
